// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { EVENT_NAME_ENUM } from "../../base/EnumIndex";
import { Tools } from "../../common/Tools";
import { EventManager } from "../../manager/EventManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Level_18_71 extends cc.Component {

    @property(cc.Node)
    bgNode: cc.Node = null;

    @property(cc.Node)
    houChe1: cc.Node = null;

    @property(cc.Node)
    houChe2: cc.Node = null;

    private speed = 0.3;
    // LIFE-CYCLE CALLBACKS:
    private anim: cc.AnimationState = null;

    onLoad() {

        EventManager.ins.on(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finish, this);

    }
    onDestroy() {
        EventManager.ins.off(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finish, this);
        this.unscheduleAllCallbacks();
    }

    start() {
        let animation = this.bgNode.getComponent(cc.Animation);
        this.anim = animation.play("bg");
        this.anim.speed = 0.3;

        let fun = () => {
            let delayTime = Tools.random(1, 10);
            this.scheduleOnce(() => {
                let type = Tools.random(1, 2)
                if (type % 2 == 0) {
                    this.cheAni1();
                } else {
                    this.cheAni2();
                }
                fun();
            }, delayTime)
        }
        this.scheduleOnce(() => {
            fun();
        }, 10);
    }

    cheAni1() {
        let animation = this.houChe1.getComponent(cc.Animation).getAnimationState("houChe1");
        animation.speed = Tools.random(0.5, 1, false);
    }

    cheAni2() {
        let animation = this.houChe2.getComponent(cc.Animation).getAnimationState("houChe2");
        animation.speed = Tools.random(0.5, 1, false);
    }

    finish(finishStr: any) {
        if (typeof finishStr != "string") {
            return;
        }
        if (finishStr.includes("speed71_")) {
            let addSpeed = Number(finishStr.split("speed71_")[1]);
            this.speed += addSpeed / 1000;
            cc.log("speed", this.speed);
            this.anim.speed = this.speed;
        }
    }

    // update (dt) {}
}
